﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace WcfServer
{
    public class FluxService : IFluxService
    {
        private SqlConnection _sqlConnection;
        public void GetDb()
        {
            _sqlConnection = new SqlConnection("Server=localhost\\SQLEXPRESS;" + "DataBase=Flux;" + "User Id=test;" + "Password=test;");
            _sqlConnection.Open();
        }

        ~FluxService()
        {
            _sqlConnection.Close();
        }

        public List<Flux> GetAllFlux()
        {
            GetDb();
            SqlCommand query = new SqlCommand();
            query.Connection = _sqlConnection;
            query.CommandText = "SELECT Id, Source, Title FROM Flux";
            SqlDataReader reader = query.ExecuteReader();

            List<Flux> temp = new List<Flux>();
            while (reader.Read())
            {
                Flux t = new Flux();
                t.Id = reader["Id"].ToString();
                t.Source = reader["Source"].ToString();
                t.Title = reader["Title"].ToString();
                temp.Add(t);
            }
            reader.Close();
            return temp;
        }
    }
}